

package com.common.ajax.server;

import java.io.Serializable;

import javax.servlet.http.HttpSession;

import org.springframework.util.Assert;

public class SessionMap implements Serializable {

	/** TODO */
	private static final long serialVersionUID = 1L;


	/** http请求的会话对象 */
	private HttpSession  httpSession = null;

	/**
	 * 构造器
	 * 
	 * @param session
	 */
	public SessionMap(HttpSession session) {
		this.httpSession = session;
	}
	/**
	 * 设置属性
	 * 
	 * @param key
	 * @param value
	 */
	public void setAttribute(String key, Object value) {
		if (key != null){
			if(httpSession != null)
				httpSession.setAttribute(key, value);
		}
	}

	/**
	 * 获得属性
	 * 
	 * @param key
	 * @return
	 */
	public Object getAttribute(String key) {
		if (key != null){
			if(httpSession != null)
				return httpSession.getAttribute(key);
			Assert.notNull(null, "both sc and httpSession are null.");
			return null;
		}
		else
			return null;
		
	}

	/**
	 * 判断对象是否存在
	 * 
	 * @param key
	 * @return
	 */
	public boolean containsAtribute(String key) {
		if (key != null ){
			if(httpSession != null)
				return httpSession.getAttribute(key)!=null;
			return false;
		}
		else
			return false;
	}

	/**
	 * 移除指定对象
	 * 
	 * @param key
	 */
	public void removeAttribute(String key) {
		if (key != null){
			if(httpSession != null)
				httpSession.removeAttribute(key);
			
		}
	}

	/**
	 * 设置属性 httpSession 的值，HttpSession类型
	 * @param httpSession the httpSession to set
	 */
	public void setHttpSession(HttpSession httpSession) {
		this.httpSession = httpSession;
	}

	/**
	 * 获取属性httpSession的值
	 * @return the httpSession : HttpSession
	 */
	public HttpSession getHttpSession() {
		return httpSession;
	}
}
